package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.domain.SeckillGood;
import cn.wolfcode.shop.domain.User;
import cn.wolfcode.shop.redis.GoodPrefixKey;
import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.redis.SeckillGoodPrefixKey;
import cn.wolfcode.shop.redis.key.KeyPrefix;
import cn.wolfcode.shop.redis.key.UserKeyPrefix;
import cn.wolfcode.shop.result.Result;
import cn.wolfcode.shop.service.ISeckillGoodService;
import cn.wolfcode.shop.service.ISeckillUserService;
import cn.wolfcode.shop.vo.GoodVo;
import com.alibaba.dubbo.config.annotation.Reference;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wolfcode-lanxw
 */
@Controller
@RequestMapping("/goods")
public class GoodController {
    @Reference
    private ISeckillGoodService seckillGoodService;
    @Reference
    private ISeckillUserService seckillUserService;
    @Autowired
    private Configuration configuration;
    @Autowired
    private RedisService redisService;
    @RequestMapping(value = "/to_list",produces = "text/html")
    @ResponseBody
    public String toList(Model model){
        String html = redisService.get(GoodPrefixKey.GOOD_LIST_PAGE, "", String.class);
        if(html!=null){
            return html;
        }
        List<GoodVo> goodVoList = seckillGoodService.queryCache();
        for(GoodVo goodVo:goodVoList){
            goodVo.setStockCount(redisService.get(SeckillGoodPrefixKey.SECKILL_STOCK,""+goodVo.getId(),Integer.class));
        }
        model.addAttribute("goodList", goodVoList);
        html = setTemplate("goods_list",model,GoodPrefixKey.GOOD_LIST_PAGE,"");
        return html;
    }
    @RequestMapping("/to_detail")
    public String toDetail(Long goodId,User user,Model model){
        if(user==null){
            return "redirect:/login.html";
        }
        model.addAttribute("user",user);
        GoodVo good = seckillGoodService.getById(goodId);
        model.addAttribute("good",good);
        return "goods_detail";
    }
    @RequestMapping("/getGoodDetail")
    @ResponseBody
    public Result<Map<String,Object>> getDetail(Long goodId,User user){
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("user",user);
        dataMap.put("good",seckillGoodService.getById(goodId));
        return Result.success(dataMap);
    }
    private String setTemplate(String viewName, Model model, KeyPrefix keyPrefix,String key){
        String result = null;
        StringWriter out = new StringWriter();
        try{
            Template template = configuration.getTemplate(viewName + ".ftl");
            template.process(model,out);
            out.close();
            result = out.toString();
            redisService.set(keyPrefix,key,result);
            return result;
        }catch(Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
